package vn.buffalocoders.rikos.ui.mylibrary;

import java.util.ArrayList;

import vn.buffalocoders.rikos.R;
import vn.buffalocoders.rikos.ui.category.CategoryActivity;
import vn.buffalocoders.rikos.ui.category.CategoryActivity.GetCategoryListTask;
import vn.buffalocoders.rikos.ui.category.CategoryActivity.GetCategoryTask;
import vn.buffalocoders.rikos.ui.common.FacebookApplication;
import vn.buffalocoders.rikos.ui.common.RikoBook;
import vn.buffalocoders.rikos.ui.common.sessions.SessionManager;
import vn.buffalocoders.rikos.ui.home.HomeActivity;
import vn.buffalocoders.rikos.ui.network.ConnectRikoServer;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Toast;

public class MyLibraryActivity extends Activity implements OnClickListener{
	
	private ArrayList<RikoBook> listbook;
	private Activity thisactivity = null;
	private GetCategoryTask mCategoryTask = null;
	private GetCategoryListTask mCategoryListTask = null;
	private SessionManager session;
	GridView gridView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_library);
		thisactivity = this;		
		
		session = new SessionManager(MyLibraryActivity.this);

		Log.i("DBT_TEST", String.format("MyLibrary userId=%s, authToken=%s", session.getUserId(), session.getAuthToken()));
		
		if (listbook == null)
		{
			mCategoryTask = new GetCategoryTask();
			mCategoryTask.execute((Void) null);
		}
		else
		{
			gridView = (GridView) findViewById(R.id.gridViewLibrary);	
			gridView.setAdapter(new BookAdapter(thisactivity, listbook));			
		}
		
		ImageView slidemenu = (ImageView) findViewById(R.id.img_slidemenu_lib);
		slidemenu.setOnClickListener(this);
		
		ImageView closelib = (ImageView) findViewById(R.id.img_close_lib);
		closelib.setOnClickListener(this);
		
		ImageView search = (ImageView) findViewById(R.id.img_search_lib);
		search.setOnClickListener(this);
	}

	public class GetCategoryTask extends AsyncTask<Void /* param */, Void /* Progress */,ArrayList<RikoBook>  /* Result */> {
		
		@Override
		protected ArrayList<RikoBook>  doInBackground(Void... param) {
			// TODO Auto-generated method stub
			return ConnectRikoServer.getBookMyLibrary(session.getAuthToken(), session.getUserId(), 0, "0", null);	
		}	
		@Override
		protected void onPostExecute(ArrayList<RikoBook> listbooks) {
		    mCategoryTask = null;
			if (listbooks == null) {
				Log.i("DBT_TEST", String.format("listbook NULL "));			
			}
			else {
				Log.i("DBT_TEST", String.format("listbook NOT NULL "));			
				listbook = listbooks;
				setUI();
			}
		}	
		@Override
		protected void onCancelled() {
			mCategoryTask = null;
		}
		
		protected void setUI(){
			Log.i("DBT_TEST", String.format("Set UI"));			
			gridView = (GridView) findViewById(R.id.gridViewLibrary);		 
			gridView.setAdapter(new BookAdapter(thisactivity,listbook));			
		}
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if(v.getId() == R.id.img_close_lib) {
			Toast.makeText(this, "Close Library", Toast.LENGTH_LONG).show();		
			return;
		}		
		if(v.getId() == R.id.img_slidemenu_lib) {
			Toast.makeText(this, "Slide menu Library", Toast.LENGTH_LONG).show();		
			return;
		}
		if(v.getId() == R.id.img_search_lib) {
			Toast.makeText(this, "Search Library", Toast.LENGTH_LONG).show();		
			return;
		}
	}
	

}
